package com.wd.myhome.view.activity

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.PopupWindow
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.opensource.svgaplayer.SVGACallback
import com.opensource.svgaplayer.SVGAParser
import com.opensource.svgaplayer.SVGAVideoEntity
import com.wd.base.App
import com.wd.current.model.Bean.DataBean
import com.wd.live.base.BaseActivity
import com.wd.mondle_home.model.adapter.FindAllGiftAdapter
import com.wd.myhome.R
import com.wd.myhome.databinding.ActivityLiveRoomBinding
import com.wd.myhome.viewmodel.LiveRoomViewModel
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter

class LiveRoomActivity :BaseActivity<LiveRoomViewModel,ActivityLiveRoomBinding>(R.layout.activity_live_room) {
    override fun dataObserver() {



        viewmodel.watchLiveData.observe(this, {
            binding.standard.setUp(it.result.liveStreamUrl, true, it.result.nickName)
            binding.standard.startPlayLogic()
        })

    }

    override fun initViews() {
        val liveStatus = intent.getIntExtra("liveStatus", 0)
        val liveId = intent.getIntExtra("liveId", 0)
        val liveTitle = intent.getStringExtra("liveTitle")
        val liveCoverUrl = intent.getStringExtra("liveCoverUrl")
        val headPic = intent.getStringExtra("headPic")
        val nickName = intent.getStringExtra("nickName")
        Glide.with(this).load(headPic)
            .circleCrop()
            .into(binding.liveWatchUserPicId)
        binding.liveWatchUserNameId.setText(nickName)

        if (liveStatus == 1) {
            //直播
            viewmodel.getWatchLiveData(App.userId, App.sessionId, liveId);
            // mViewModel.getWatchLive(MyApp.userId, MyApp.sessionId, liveId)
        } else if (liveStatus == 2) {
            binding.standard.setUp(
                "http://10.59.9.24/live-platform/recordVideo/${liveId}",
                true,
                liveTitle
            )
            binding.standard.startPlayLogic()


        }

        //===========================查询礼物列表==========
        viewmodel.getFindAllGiftData()
        binding.liveWatchGiftBtnId.setOnClickListener {
            sendGift()



        }
        //=============================送礼物方法

    }
    var labsAllGift = mutableListOf<DataBean.FindAllGiftBean.Result>()
    val giftArr = mutableListOf<String>(
        "hongbao.svga",
        "aixin.svga",
        "shuiguo.svga",
        "huatong.svga",
        "huangguan.svga",
        "huojian.svga"
    )

    @SuppressLint("MissingInflatedId")
    fun sendGift() {
        //=========================加载布局
        val view = LayoutInflater.from(this).inflate(R.layout.gift_item_popup, null)
        //=========================获取PopupWindow对象 设置宽度为match为 高度自适应
        val popWnd: PopupWindow = PopupWindow(
            view,
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            true
        )
        popWnd.contentView = view
        //==========================设置位置
        popWnd.showAtLocation(view, Gravity.BOTTOM, 0, 0)
        popWnd.isFocusable = false
        //==========================礼物列表
        val giftRv = view?.findViewById<RecyclerView>(R.id.gift_rv)
        val findAllGiftAdapter =
            FindAllGiftAdapter(this, R.layout.gift_list_popup_item, labsAllGift)
        //==========================设置适配器和列表类型
        giftRv?.adapter = findAllGiftAdapter
        giftRv?.layoutManager = GridLayoutManager(this, 3)
        //==========================给数据集添加数据
        viewmodel.findAllGiftData.observe(this) {
            it.result.let { it1 ->
                if (it1 != null) {
                    labsAllGift.clear()
                    labsAllGift.addAll(it1)
                }
            }
            findAllGiftAdapter?.notifyDataSetChanged()
        }
        //==============================赠送按钮
        val giveGiftId = view?.findViewById<Button>(R.id.give_gift_id)
        //==============================价格显示按钮
        val priceGiftPopupId = view?.findViewById<Button>(R.id.price_gift_popup_id)
        //==============================条目点击事件
        findAllGiftAdapter.setOnItemClickListener(object :
            MultiItemTypeAdapter.OnItemClickListener {
            override fun onItemClick(p0: View?, p1: RecyclerView.ViewHolder?, p2: Int) {
                findAllGiftAdapter.anInt = p2
                giveGiftId?.setBackgroundColor(R.drawable.back)
                giveGiftId?.setBackgroundResource(R.drawable.back)
                priceGiftPopupId?.visibility = View.VISIBLE
                priceGiftPopupId?.text = "" + labsAllGift[findAllGiftAdapter.anInt].price
                findAllGiftAdapter.notifyDataSetChanged()
            }

            override fun onItemLongClick(
                p0: View?,
                p1: RecyclerView.ViewHolder?,
                p2: Int
            ): Boolean {
                return true
            }
        })
        //==============================赠送点击事件
        giveGiftId?.setOnClickListener(View.OnClickListener {
            popWnd.dismiss()
            binding.liveWatchPlaySvgaId.visibility = View.VISIBLE
            binding.liveWatchPlaySvgaId.loops = 1
            SVGAParser(this).decodeFromAssets(giftArr[findAllGiftAdapter.anInt], object :
                SVGAParser.ParseCompletion {
                override fun onComplete(videoItem: SVGAVideoEntity) {
                    if (binding.liveWatchPlaySvgaId != null) {
                        binding.liveWatchPlaySvgaId.setVideoItem(videoItem)
                        binding.liveWatchPlaySvgaId.startAnimation()
                    }
                }

                override fun onError() {}
            })
            //==============================监听事件
            binding.liveWatchPlaySvgaId.callback.apply {
                object : SVGACallback {
                    override fun onFinished() {
                        binding.liveWatchPlaySvgaId.stopAnimation()
                        binding.liveWatchPlaySvgaId.visibility = View.GONE
                    }

                    override fun onPause() {

                    }

                    override fun onRepeat() {

                    }

                    override fun onStep(frame: Int, percentage: Double) {

                    }
                }
            }
        })


    }
}